<?php

/**
 *
 * Abstract controller used for everything common
 * to all the controllers.
 * jQuery is enabled by default.
 * jQueryUI is disabled by default.
 * @author Théo Chamley
 *
 */
abstract class CodeBar_AbstractController extends Zend_Controller_Action {

	private $jQueryVersion;
	private $jQueryUIVersion;
	private $session;
	private $_redirector;
	
	protected function session() {
		return $this->session;
	}
	
	public function init() {
		$this->session = new Zend_Session_Namespace("codebar");
		$this->_redirector = $this->_helper->getHelper('Redirector');
		
		$this->view->headTitle()->setSeparator(' > ');
		$this->view->headTitle("CodeBar");
		$this->view->headLink()->appendStylesheet('/css/main/main.css');

		/*
		 * jQuery
		 */
		$config = $this->getInvokeArg('bootstrap')->getOptions();
		$this->jQueryVersion = $config['version']['jquery'];
		$this->jQueryUIVersion = $config['version']['jqueryui'];

		$this->_helper->layout->setLayout('layout_jquery');
		$this->view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');
		$helperUrl = new Zend_View_Helper_BaseUrl();
		$this->view->jQuery()->setLocalPath($helperUrl->baseUrl()."/js/jquery/jquery-".$this->jQueryVersion.".min.js")
		->setUiLocalPath($helperUrl->baseUrl()."/js/jquery/jquery-ui-".$this->jQueryUIVersion.".custom.min.js")
		->enable()
		->uiDisable();
		
		/*
		 * Handling errors and messages
		 */
		$this->view->error = $this->getError();
		$this->view->message = $this->getMessage();
		
		/*
		 * Main JS file, activating tooltips, displaying messages and errors
		 */
		$this->view->headScript()->appendFile($helperUrl->BaseUrl()."/js/main.js","text/javascript");
		$this->view->headScript()->appendFile($helperUrl->BaseUrl()."/js/jquery/jquery.tools.min.js","text/javascript");
	}

	protected function jQueryDisable() {
		$this->jQueryUIDisable();
		$this->view->jQuery()->disable();
		return $this;
	}

	protected function jQueryEnable() {
		$this->view->jQuery()->enable();
		return $this;
	}

	protected function jQueryUIDisable() {
		$this->view->jQuery()->uiDisable();
		/*
		 * Removing the jQuery UI CSS
		 */
		$iterator = $this->view->headLink()->getIterator();
		$toRemove = NULL;
		foreach($iterator as $key => $i) {
			$tmp = get_object_vars($i);
			if (stristr($tmp['href'], "jquery")) {
				$toRemove = $key;
			}
		}
		if(isset($toRemove)) {
			$this->view->headLink()->offsetUnset($toRemove);
		}
		return $this;
	}

	protected function jQueryUIEnable() {
		$this->view->jQuery()->uiEnable();
		/*
		 * Adding the jQuery UI CSS
		 */
		$helperUrl = new Zend_View_Helper_BaseUrl();
		$this->view->headLink()->prependStylesheet($helperUrl->BaseUrl()."/css/jqueryui/jquery-ui-".$this->jQueryUIVersion.".custom.css");
		return $this;
	}
	
	/**
	 * 
	 * When called, this function redirects the navigator
	 * to the action $action of the controller $controller.
	 * If $controller is null, the current controller is used,
	 * if $action is null, the current action is used.
	 * This is mainly used after the treatment of a form (after
	 * a POST). You redirect the navigator to the same page to avoid
	 * the re-submission of the form if the user presses F5.
	 * @param String $action
	 * @param String $controller
	 */
	protected function redirect($action = null, $controller = null, $params = array()) {
		$this->_redirector->setCode(303)
				->gotoSimple($action == null ? $this->getRequest()->getActionName() : $action, $controller, null, $params);
	}
	
	/*
	 * Messages to user management.
	 * Messages and errors are stored in a session variable.
	 * Once accessed, they are deleted.
	 */
	/**
	 * 
	 * Stores an error that will be displayed on the
	 * next page loaded by the user.
	 * @param string $e
	 */
	protected function error($e = "") {
		if(!isset($this->session()->error)) {
			$this->session()->error = array();
		}
		$this->session()->error[] = $e;
		return $this;
	}
	
	/**
	 * 
	 * Stores a message that will be displayed on the
	 * next page loaded by the user.
	 * @param string $m
	 */
	protected function message($m = "") {
		if(!isset($this->session()->message)) {
			$this->session()->message = array();
		}
		$this->session()->message[] = $m;
		return $this;
	}
	
	/**
	 * 
	 * Returns the error stored in the session.
	 * Can return null.
	 * Once returned, the error is deleted.
	 */
	protected function getError() {
		$e = $this->session()->error;
		$this->session()->error = array();
		return $e;
	}
	
	/**
	 * 
	 * Returns the message stored in the session.
	 * Can return null.
	 * Once returned, the message is deleted.
	 */
	protected function getMessage() {
		$m = $this->session()->message;
		$this->session()->message = array();
		return $m;
	}

	abstract public function indexAction();

}